﻿using System;
using System.Configuration;
using System.Diagnostics;

namespace PKTools
{
    public enum LogSeverity
    {
        Debug = 0,
        Info = 1,
        Warning = 2,
        Error = 3
    }

    public static class Log
    {
        public static readonly int
            Level;

        static Log()
        {
            Trace.AutoFlush = true;

            Level = ConfigurationSettings.AppSettings["PKTools.Log.Level"].SafeToInt((int)LogSeverity.Debug);

            string
                file = ConfigurationSettings.AppSettings["PKTools.Log.File"];

            if (!string.IsNullOrEmpty(file))
                Trace.Listeners.Add(new LogTraceWriter(file));
        }

        public static void Entry(LogSeverity severity, string format, params object[] objs)
        {
            if (Level > (int)severity)
                return;

            Trace.WriteLine(DateTime.UtcNow.ToDetailString() + " - " + severity + ": " + string.Format(format, objs));
        }
    }
}
